﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Models;
using SqlSugar;

namespace DAL.Project
{
    public class ProjectDao : BaseDaoPro<Models.Projects>, IProjectDao
    {
        string selectSql = @"select *,
(select count(*) from ProjectArtists a where a.ProjectID = p.ProjectID) ApplyCount
from Projects p";

        /// <summary>
        /// 从数据库中，删除一条需求
        /// </summary>
        /// <param name="id">需求id</param>
        /// <returns>
        /// true为删除成功，反之则失败
        /// </returns>
        public bool DeleteProjectById(int id)
        {
            db.Deleteable<Models.ProjectArtists>()
                .In(it => it.ProjectID, id)
                .ExecuteCommandHasChange();
            return db.Deleteable<Models.Projects>()
                .In(it => it.ProjectID ,id)
                .ExecuteCommandHasChange();
        }

        public override bool Delete(int[] ids)
        {
            foreach (int id in ids)
            {
                DeleteProjectById(id);
            }
            return true;
        }

        public override List<Models.Projects> QueryForList(List<IConditionalModel> searchParams, ref int totalCount, int pageIndex = 1, int pageSize = 15)
        {
            return db.SqlQueryable<Models.Projects>(selectSql)
                .Mapper(it => it.Owner, it => it.OwnerID, it => it.Owner.UserID)
                .Where(searchParams)
                .ToPageList(pageIndex, pageSize, ref totalCount);
        }
        public override List<Models.Projects> QueryForList(List<IConditionalModel> searchParams, string orderBy, ref int totalCount, int pageIndex = 1, int pageSize = 15)
        {
            return db.SqlQueryable<Models.Projects>(selectSql)
                .Mapper(it => it.Owner, it => it.OwnerID, it => it.Owner.UserID)
                .Where(searchParams)
                .OrderBy(orderBy)
                .ToPageList(pageIndex, pageSize, ref totalCount);
        }

        public List<Projects> GetList(ref int totalCount, int pageIndex = 1, int pageSize = 12, string orderBy = "CreatedAt Desc")
        {
            return db.SqlQueryable<Models.Projects>(selectSql)
                .Mapper(it => it.Owner, it => it.OwnerID, it => it.Owner.UserID)
                .OrderBy(orderBy)
                .ToPageList(pageIndex, pageSize, ref totalCount);
        }

        public List<Projects> GetListByTag(ref int totalCount, string[] tag, int pageIndex = 1, int pageSize = 12, string orderBy = "CreatedAt Desc")
        {
            Expressionable<Models.Projects> exp = new Expressionable<Projects>();
            foreach (var item in tag)
            {
                exp.And(it => it.Tag.Contains(item));
            }

            return db.SqlQueryable<Models.Projects>(selectSql)
                  .Mapper(it => it.Owner, it => it.OwnerID, it => it.Owner.UserID)
                  .Where(exp.ToExpression())
                  .OrderBy(orderBy)
                  .ToPageList(pageIndex, pageSize, ref totalCount);
        }

        public List<Projects> GetListByUserId(ref int totalCount, int userId, int pageIndex = 1, int pageSize = 12, string orderBy = "CreatedAt Desc")
        {
            Expressionable<Models.Projects> exp = new Expressionable<Projects>();
            exp.And(it => it.OwnerID == userId);

            return db.SqlQueryable<Models.Projects>(selectSql)
                  .Mapper(it => it.Owner, it => it.OwnerID, it => it.Owner.UserID)
                  .Where(exp.ToExpression())
                  .OrderBy(orderBy)
                  .ToPageList(pageIndex, pageSize, ref totalCount);
        }

        /// <summary>
        /// 通过需求id，从数据库获取需求详情
        /// </summary>
        /// <param name="id">需求id</param>
        /// <returns>需求实体类</returns>
        public Projects GetProjectById(int id)
        {
            return db.SqlQueryable<Models.Projects>(selectSql)
                .Mapper(it => it.Owner, it => it.OwnerID, it => it.Owner.UserID)
                .First(it => it.ProjectID == id);
        }

        /// <summary>
        /// 插入需求到数据库
        /// </summary>
        /// <param name="project">需求实体类</param>
        public void InsertProject(Projects project)
        {
            db.Insertable<Models.Projects>(project).ExecuteCommandIdentityIntoEntity();
        }

        /// <summary>
        /// 更新需求到数据库
        /// </summary>
        /// <param name="project">需求实体类</param>
        /// <returns>true为更新成功，反之则失败</returns>
        public bool UpdateProject(Projects project)
        {
            return db.Updateable<Models.Projects>(project).ExecuteCommandHasChange();
        }
    }
}
